class PSPawnEnemyRandom extends PSPawn
	placeable;

enum EBehavior
{
	BEHAVIOR_Coward,
	BEHAVIOR_Brave,
	BEHAVIOR_Aggresive
};

enum EMovingSpeed
{
	SPEED_Idle,
	SPEED_Walking,
	SPEED_Running
};

// General vars
var EBehavior Profile;
var float CoverProb;
var bool bBeingAttacked;
var bool bJustTouched;
var bool bTouched;
var bool bPatroller;
var float CheckTime;
var float CoverTime;
var float UpsetTime;
var bool bAlreadyPrayed;
var float MeleeDistance;

// Move vars
var float WalkingSpeed;
var float RunningSpeed;
var int MaxDistanceToPlayer;
var int MinDistanceToPlayer;
var int MaxNodeDistance;
var int MaxSightDistance;

// Noise vars
var float FootstepHearingThreshold;
var float WeaponFireHearingThreshold;
var float BulletImpactHearingThreshold;
var float FightAlertHearingThreshold;
var float InvisibleTransitionHearingThreshold;

//Bone names
var name boneHead;
var name boneNeck;

// Audio
var AudioComponent acPeace;
var AudioComponent acWarFight;
var AudioComponent acWarLost;
var AudioComponent acDeath;

var PSInventoryManager im;

var bool bChatting;

function SetProfile(int ProfileIndex)
{
	// Assign profile
	switch(ProfileIndex)
	{
	case 0:
		Profile = BEHAVIOR_Coward;
		break;
	case 1:
		Profile = BEHAVIOR_Brave;
		break;
	case 2: 
		Profile = BEHAVIOR_Aggresive;
		break;
	}

	// Create inventory
	im = PSInventoryManager(InvManager);

	// Assing common values
	WalkingSpeed = 50.0;
	UpsetTime = 5.0;
	CheckTime = 10.0;
	MaxNodeDistance = 2048;
	FootstepHearingThreshold = 200;
	WeaponFireHearingThreshold = 1500;
	BulletImpactHearingThreshold = 500;
	FightAlertHearingThreshold = 750;
	InvisibleTransitionHearingThreshold = 150;
	MaxSightDistance = 750;
	CurrentPosture = EPosture.POSTURE_Walking;
	MeleeDistance = 100.0;
		
	// Assing profile specific values
	switch(Profile)
	{
	case EBehavior.BEHAVIOR_Coward:
		im.wPistol = spawn(class'PSWeaponPistol', im.Owner);
		im.AddInventory(im.wPistol);
		CurrentWeapon = EWeapon.WEAPON_Pistol;
		CoverProb = 0.9;
		Health = 100;
		RunningSpeed = 200;
		MaxDistanceToPlayer = 2048;
		MinDistanceToPlayer = 512;
		CoverTime = 5.0;
		acPeace.SoundCue = SoundCue'PS_gameplay_expressions.Peace.Cow_peace';
		acWarFight.SoundCue = SoundCue'PS_gameplay_expressions.War.Cow_war_fight';
		acWarLost.SoundCue = SoundCue'PS_gameplay_expressions.War.Cow_war_lost';
		acDeath.SoundCue = SoundCue'PS_gameplay_expressions.Death.Cow_death';		
		break;
	case EBehavior.BEHAVIOR_Brave:
		im.wFusil = spawn(class'PSWeaponFusil', im.Owner);
		im.AddInventory(im.wFusil);
		CurrentWeapon = EWeapon.WEAPON_Rifle;
		CoverProb = 0.7;
		Health = 200;
		RunningSpeed = 175;
		MaxDistanceToPlayer = 1024;
		MinDistanceToPlayer = 256;
		CoverTime = 5.0;
		acPeace.SoundCue = SoundCue'PS_gameplay_expressions.Peace.Bra_peace';
		acWarFight.SoundCue = SoundCue'PS_gameplay_expressions.War.Bra_war_fight';
		acWarLost.SoundCue = SoundCue'PS_gameplay_expressions.War.Bra_war_lost';
		acDeath.SoundCue = SoundCue'PS_gameplay_expressions.Death.Bra_Death';
		break;
	case EBehavior.BEHAVIOR_Aggresive:
		im.wFusil = spawn(class'PSWeaponFusil', im.Owner);
		im.AddInventory(im.wFusil);
		CurrentWeapon = EWeapon.WEAPON_Rifle;
		CoverProb = 0.3;
		Health = 300;
		RunningSpeed = 175;
		MaxDistanceToPlayer = 1024;
		MinDistanceToPlayer = 256;
		CoverTime = 3.0;
		acPeace.SoundCue = SoundCue'PS_gameplay_expressions.Peace.Agg_peace';
		acWarFight.SoundCue = SoundCue'PS_gameplay_expressions.War.Agg_war_fight';
		acWarLost.SoundCue = SoundCue'PS_gameplay_expressions.War.Agg_war_lost';
		acDeath.SoundCue = SoundCue'PS_gameplay_expressions.Death.Agg_Death';
		break;
	}
}


event PostBeginPlay()
{
	super.PostBeginPlay();

	// Initialize enemy parameters according to the three available profiles
	SetProfile(Rand(3));
}

function SetMovingSpeed(EMovingSpeed speed)
{
	switch(speed)
	{
	case EMovingSpeed.SPEED_Idle:
		GroundSpeed = 0.0;
		break;
	case EMovingSpeed.SPEED_Walking:
		GroundSpeed = WalkingSpeed;
		break;
	case EMovingSpeed.SPEED_Running:
		GroundSpeed = RunningSpeed;
		break;
	}
}	

function SetBeingAttacked()
{
	bBeingAttacked = true;
	ClearTimer('ResetBeingAttacked');  // Stop previous timer if exists
	SetTimer(1.0, false, 'ResetBeingAttacked');
}

function ResetBeingAttacked()
{
	bBeingAttacked = false;
}

// Ideally, this event would take place in the AI controller, so it could be defined in specific states.
// However, Bump (and Touch) only work if defined in Pawn-extended classes, so we hack it to perform properly.
event Bump(Actor Other, PrimitiveComponent OtherComp, Vector HitNormal)
{
	if( (PSPawnDidrik(Other) != none) && !bJustTouched)
	{
		PSAIControllerBase(Controller).TouchedByPlayer(HitNormal);
		bJustTouched = true;
		SetTimer(3.0, false, 'ResetJustTouched');
	}
	if(PSPawnEnemyRandom(Other) != none)
	{
		PSAIControllerBase(Controller).BlockedByComrade();
	}
}

function ResetJustTouched()
{
	bJustTouched = false;
}


event TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
	local Controller Killer;

	// Avoid enemies killing their comrades
	if(PSPlayerController(InstigatedBy) == none)
	{
		return;
	}

	// If already dead, out
	if(Health <= 0)
	{
		return;
	}	

	if(HitInfo.BoneName==boneHead) Damage = Health+10;
	else if(HitInfo.BoneName==boneNeck) Damage = Damage*3;
	
	Health -= Damage;
	if (HitLocation == vect(0,0,0))
	{
		HitLocation = Location;
	}

	if(Health <= 0)
	{
		// Killed
		Killer = SetKillInstigator(InstigatedBy, DamageType);
		Died(Killer, damageType, HitLocation);
		Controller.GotoState('Dying');
		acDeath.Play();
		bDead = true;
	}

	SetBeingAttacked();
}

function PerformMelee()
{
	local PSPawnDidrik PlayerPawn;
	local float tDist, disX, disY, disZ;
	local Vector hitDir;

	PlayerPawn = PSPawnDidrik(GetALocalPlayerController().Pawn);

	if(FastTrace(PlayerPawn.Location, Location))
	{
		disX = Square(Abs(PlayerPawn.Location.X - Location.X));
		disY = Square(Abs(PlayerPawn.Location.Y - Location.Y));
		disZ = Square(Abs(PlayerPawn.Location.Z - Location.Z));
	
		tDist = Abs(Sqrt(disX + disY + disZ));
			
		if(tDist <= PSPlayerController(PlayerPawn.Controller).MinMeleeDistance)
		{
			hitDir = PlayerPawn.Location - Location;

			PlayerPawn.TakeDamage(50,PSPlayerController(Controller),PlayerPawn.Location,hitDir,class'PSMeleeDamage',,self);

			PlaySound(SoundCue'PS_sounds.Melee.AU_Melee_cue', false, true, , Location, true);
		}
	}
}

event Tick(float DeltaTime)
{
	local float CurrentPitch;
	local Rotator RotationToTarget;

	if( Controller.IsInState('Evade') || Controller.IsInState('TakeCover') || Controller.IsInState('Attack') )
	{
		RotationToTarget = Rotator(PSAIControllerBase(Controller).Target.Location - Location);
		CurrentPitch = NormalizeRotAxis(RotationToTarget.Pitch);
		AimOffsetNode.Aim.Y = CurrentPitch / ((CurrentPitch > 0) ? ViewPitchMax : -ViewPitchMin);
	}
	else
	{
		AimOffsetNode.Aim.Y = 0.0;
	}
}

function PlayPeaceAudio()
{
	acPeace.Play();
}

function PlayWarFightAudio()
{
	acWarFight.Play();
	BroadcastNoise(Location, ESoundType.SOUND_FightAlert);
}

function PlayWarLostAudio()
{
	acWarLost.Play();
}

DefaultProperties
{
	 Begin Object class=SkeletalMeshComponent Name=PSPawnSkeletalMesh
 		SkeletalMesh=SkeletalMesh'CH_Infiltrator.SK_CH_Infiltrator'
		AnimSets(0)=AnimSet'CH_Infiltrator.animation.AS_CH_Infiltrator'
		AnimTreeTemplate=AnimTree'CH_Infiltrator.AT_CH_Infiltrator'
		PhysicsAsset=PhysicsAsset'CH_Infiltrator.SK_CH_Infiltrator_Physics'
		LightEnvironment=MyLightEnvironment
		HiddenGame=false 
		HiddenEditor=false
		CollideActors=true
		bHasPhysicsAssetInstance=true 
		BlockActors=true
		BlockNonZeroExtent=true
		BlockZeroExtent=true
		//BlockRigidBody=true
    End Object
	Mesh=PSPawnSkeletalMesh	
    Components.Add(PSPawnSkeletalMesh)
	
	Begin Object Name=CollisionCylinder
		bDrawBoundingBox=false
		CollisionRadius=+0010.000000
		CollisionHeight=+0045.000000
		BlockNonZeroExtent=false
		BlockZeroExtent=false
		BlockActors=true
		CollideActors=false
    end object
	CollisionComponent=CollisionCylinder
	CylinderComponent=CollisionCylinder
	Components.Add(CollisionCylinder)

	InventoryManagerClass=class'PSGame.PSInventoryManager'
    ControllerClass=class'PSGame.PSAIControllerBase'

	GroundSpeed=100.0
	bJumpCapable=false
	bCanJump=false
	bBeingAttacked=false

	RotationRate=(Pitch=50000,Yaw=50000,Roll=50000)

	boneHead=infiltrator_Head
	boneNeck=infiltrator_Neck2

	bJustTouched=false
	bAlreadyPrayed=false

	// Add audio componnets
	Begin Object Class=AudioComponent Name=AudioComp1   
    End Object    
	acPeace = AudioComp1
    Components.Add(AudioComp1)

	Begin Object Class=AudioComponent Name=AudioComp2   
    End Object    
	acWarFight = AudioComp2
    Components.Add(AudioComp2)

	Begin Object Class=AudioComponent Name=AudioComp3  
    End Object    
	acWarLost = AudioComp3
    Components.Add(AudioComp3)

	Begin Object Class=AudioComponent Name=AudioComp4   
    End Object    
	acDeath = AudioComp4
    Components.Add(AudioComp4)

	bChatting=false
}
